<?php

if (!defined('CORE_PATH'))
    die('Access Denied');

class SpotlightsModule extends Module {

    protected $models = array('Spotlight');
    protected $helpers = array('File', 'Request');

    function __construct() {
        parent::__construct(dirname(__FILE__));
    }

    public function index() {
        $spotlights = $this->Spotlight->find(array(
            'filter' => array('SiteID' => SITE_ID)
        ));

        $this->set('spotlights', $spotlights);
    }

    public function add() {
        $this->autoLayout = false;

        if (isset($_POST['menu_header'])) {
            $this->autoRender = false;

            $dirname = ASSETS_PATH . "images/" . SITE_TAG . "/spotlight/";
            $result = $this->File->uploadFile($dirname, "spotlight" . date('YmdHis'));

            $spotlight = new Spotlight(array(
                'SiteID' => SITE_ID,
                'menu_header' => $_POST['menu_header'],
                'menu_subheader' => $_POST['menu_subheader'],
                'spotlight' => str_replace(ASSETS_PATH, "../assets/", $result['file']),
                'description' => $_POST['description'],
                'spotlight_url' => $_POST['spotlight_url'],
                'date' => date('Y-m-d'),
                'approved' => isset($_POST['approved']) ? "true" : "false"
            ));

            $spotlight->save();

            $this->redirect(array('action' => 'index'));
        }
    }

    public function edit($id) {
        $this->autoLayout = false;

        if (isset($_POST['menu_header'])) {
            $this->autoRender = false;

            $spotlight = new Spotlight(array(
                'SiteID' => SITE_ID,
                'menu_header' => $_POST['menu_header'],
                'menu_subheader' => $_POST['menu_subheader'],
                'spotlight' => $_POST['spotlight'],
                'description' => $_POST['description'],
                'spotlight_url' => $_POST['spotlight_url'],
                'date' => date('Y-m-d'),
                'approved' => isset($_POST['approved']) ? "true" : "false",
                'id' => $_POST['id']
            ));

            $spotlight->save();

            return;
        }

        $spotlight = new Spotlight($id);
        $this->set('spotlight', $spotlight);
    }

    public function delete($id) {
        $this->autoRender = false;

        if (!defined("IN_CMS")) {
            trigger_error("Spotlight Deletion Is Only Allowed From CMS Interface");
            return;
        }

        if ($this->Request->isAjax()) {
            $spotlight = new Spotlight($id);
            $spotlight->delete();
        }
    }

    public function delete_selected() {
        $this->autoRender = false;

        if (isset($_POST['ids'])) {
            $this->Spotlight->deleteAll(array("id IN" => explode(",", $_POST['ids'])));
        }
    }

    public function publish() {
        $this->autoRender = false;

        if (isset($_POST['id'])) {
            $spotlight = new Spotlight($_POST['id']);
            $spotlight->publish($_POST['action'] == "publish");
        }
    }

    /**
     * Returns a Spotlight object based on the ID or array options specified
     * 
     * @param mixed $data
     * @return Spotlight
     */
    public function getSpotlight($data) {
        if (intval($data) > 0) {
            $spotlight = new Spotlight($data);
            return $spotlight;
        }

        if (is_array($data)) {
            return $this->Spotlight->findFirst($data);
        }
    }

    /**
     * Returns an Array of Spotlight objects for the site
     *
     * @param int $siteid ID of the site
     * @param int $start the start index to load the data from
     * @param int $limit the total number of records to load
     * @return array an array of Spotlight objects
     */
    public function getSpotlights($start = 0, $limit = NULL, $options = NULL) {

        $default = array(
            'filter' => array(
                'SiteID' => SITE_ID,
                'approved' => 'true'
            ),
            'order' => array('id' => 'desc'),
            'offset' => $start,
            'limit' => $limit
        );

        if (isset($options) && is_array($options)) {
            $default = array_merge($default, $options);
        }

        return $this->Spotlight->find($default);
    }

}

?>